// 文章详情页

<template>
  <div class="articleDetail">
    <!-- 顶部 -->
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" class="back" />
        <span class="iconfont iconnew new" @click="$router.push({path:'/index'})"></span>
      </div>
      <span
        class="right focus"
        @click="followCurrentUser"
        :class="{active:article.has_follow}"
      >{{article.has_follow?'已关注':'关注'}}</span>
    </div>
    <!-- 内容模块 -->
    <div class="cont">
      <div class="cont_title">{{article.title}}</div>
      <div class="desc">
        <span class="source">{{article.nickname}}</span>
        <span class="time">2019-11-10</span>
      </div>
      <div class="cont_main" v-html="article.content" v-if="article.type == 1"></div>
      <!-- type为2时，添加视频控制 -->
      <video :src="article.content" v-if="article.type == 2" controls muted autoplay></video>
      <!-- 内容底下 点赞与微信 -->
      <div class="cont_footer">
        <span class="like" :class="{active:article.has_like}" @click="likeThisArt">
          <van-icon name="good-job-o" />
          {{article.like_length}}
        </span>
        <span class="wechat">
          <van-icon name="chat" class="w" />微信
        </span>
      </div>
    </div>
    <!-- 精彩跟帖模块 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <!-- 跟帖用户 -->
      <div class="branch">
        <div class="branch_header">
          <div class="observer">
            <img src="../assets/167.jpg" class="observer_photo" />
            <div class="right">
              <span class="observer_name">我是白球杨</span>
              <i class="observe_time">2019-10-10</i>
            </div>
          </div>
          <div class="reply">回复</div>
        </div>
        <div class="observer_cont">有点道理</div>
      </div>
      <div class="more">更多跟贴</div>
      <!-- <div class="keeps_footer">
      <input type="text" class="write" placeholder="写跟帖" />-->
      <!-- 右侧小组件 -->
      <!-- <div class="widget">
          <van-icon name="chat-o" />
          <van-icon name="star-o" />
          <van-icon name="share-o" />
      </div>-->
      <!-- </div> -->

      <!-- 使用组件 ; 父亲传子 -->
      <commentFooter :post="article"></commentFooter>
    </div>
  </div>
</template>

<script>
import { getArtDetail, likeArt } from "@/apis/articles";
import { followUser, unfollowUser } from "@/apis/users";
// 引入组件
import commentFooter from "@/components/commentFooter";
export default {
  // 注入组件
  components: {
    commentFooter,
  },
  data() {
    return {
      article: {
        user: {},
      },
    };
  },
  async mounted() {
    // 根据id获取文章详情，实现文章详情的渲染
    let res = await getArtDetail(this.$route.params.id);
    console.log(res);
    this.article = res.data.data;
  },
  methods: {
    async followCurrentUser() {
      let res;
      // has_follow得值是布尔值
      if (this.article.has_follow) {
        // 取消关注用户
        res = await unfollowUser(this.article.user.id);
      } else {
        // 关注用户
        res = await followUser(this.article.user.id);
      }
      console.log(res);
      // 重置has_follow
      this.article.has_follow = !this.article.has_follow;
      console.log(this.article.has_follow);
      this.$toast.success(res.data.message);
    },
    async likeThisArt() {
      let res = await likeArt(this.article.id);
      console.log(res);
      // 重置has_follow
      this.article.has_like = !this.article.has_like;
      // 如果点赞like，点赞数量加一
      if (this.article.has_like) {
        // 先自加，在赋值
        ++this.article.like_length;
      } else {
        --this.article.like_length;
      }
      this.$toast.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scoped>
.articleDetail {
  .header {
    height: 50 * 100vw/360;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      height: 50 * 100vw/360;
      line-height: 50 * 100vw/360;
      .back {
        position: absolute;
        top: 16 * 100vw/360;
        left: 10 * 100vw/360;
        font-size: 14 * 100vw/360;
      }
      .new {
        position: absolute;
        top: 0;
        left: 30 * 100vw/360;
        font-size: 45 * 100vw/360;
      }
    }
    .right.focus {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      // top: 0;
      right: 15 * 100vw/360;
      width: 70 * 100vw/360;
      height: 25 * 100vw/360;
      border: 1 * 100vw/360 solid #ccc;
      border-radius: 20 * 100vw/360;
      &.active {
        background: #f00;
        color: #fff;
        border: none;
      }
    }
  }
  .cont {
    padding: 10 * 100vw/360;
    &_title {
      font-size: 20 * 100vw/360;
      font-weight: 600;
      margin-bottom: 8 * 100vw/360;
    }
    .desc {
      margin-bottom: 20 * 100vw/360;
      .source,
      .time {
        color: #cdcdcd;
      }
      .time {
        margin-left: 15 * 100vw/360;
      }
    }
    &_main {
      line-height: 25 * 100vw/360;
      overflow: hidden;
      text-overflow: ellipsis;
      /* 弹性伸缩盒子模型显示 */
      display: -webkit-box;
      /* 限制在一个块元素显示的文本的行数 */
      -webkit-line-clamp: 7;
      /* 设置或检索伸缩盒对象的子元素的排列方式 */
      -webkit-box-orient: vertical;
      /deep/img {
        width: 100%;
      }
    }
    video {
      width: 100%;
    }
    &_footer {
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 20 * 100vw/360;
      .like,
      .wechat {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 70 * 100vw/360;
        height: 20 * 100vw/360;
        border: 1 * 100vw/360 solid #000;
        border-radius: 20 * 100vw/360;
        .van-icon {
          font-size: 14 * 100vw/360;
          margin-right: 5 * 100vw/360;
        }
      }
      .like {
        &.active {
          border: 1 * 100vw/360 solid #f00;
        }
      }
      .wechat {
        .van-icon {
          color: rgb(52, 247, 52);
        }
      }
    }
  }
  .keeps {
    position: relative;
    border-top: 5 * 100vw/360 solid #ccc;
    padding-top: 20 * 100vw/360;
    h2 {
      font-size: 20 * 100vw/360;
      font-weight: 400;
      text-align: center;
    }
    .branch {
      padding: 10 * 100vw/360;
      border-bottom: 1 * 100vw/360 solid #ccc;
      &_header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 50 * 100vw/360;
        .observer {
          display: flex;
          justify-content: space-between;
          align-items: center;
          &_photo {
            width: 40 * 100vw/360;
            height: 40 * 100vw/360;
            border-radius: 50%;
            margin-right: 10 * 100vw/360;
          }
          .right {
            display: flex;
            justify-content: space-between;
            flex-direction: column;
            .observer_name {
              font-size: 16 * 100vw/360;
              color: slategray;
            }
            .observe_time {
              font-size: 14 * 100vw/360;
              color: #ccc;
            }
          }
        }
        .reply {
          font-size: 16 * 100vw/360;
          color: slategray;
        }
      }
      .observer_cont {
        font-size: 14 * 100vw/360;
        padding: 10 * 100vw/360;
      }
    }
    .more {
      width: 100 * 100vw/360;
      height: 30 * 100vw/360;
      line-height: 30 * 100vw/360;
      text-align: center;
      border: 1 * 100vw/360 solid #ccc;
      border-radius: 15 * 100vw/360;
      margin: 20 * 100vw/360 auto;
      font-size: 14 * 100vw/360;
    }
    &_footer {
      position: fixed;
      bottom: 15 * 100vw/360;
      left: 0;
      right: 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: linen;
      border-radius: 20 * 100vw/360 20 * 100vw/360 0 0;
      height: 40 * 100vw/360;
      padding: 7 * 100vw/360 10 * 100vw/360;
      input.write {
        flex: 4;
        height: 30 * 100vw/360;
        background: #bcb3a8;
        border: none;
        border-radius: 20 * 100vw/360;
        text-indent: 15 * 100vw/360;
      }
      .widget {
        display: flex;
        align-items: center;
        .van-icon {
          font-size: 24 * 100vw/360;
          color: slategrey;
          margin: 0 7 * 100vw/360;
        }
      }
    }
  }
}
</style>